# coding=utf-8
# 全世界最帅的帅哥
from flask import current_app
from ysgg1_club_comment.modules.auth_permission import AuthPermission
from flask_restful import reqparse
from ysgg1_club_comment.modules import db
from sqlalchemy.exc import OperationalError, SQLAlchemyError
from ysgg1_club_comment.utils.fbmsg import FBMsg
from flask import request


def create_permission():
    try:
        parser = reqparse.RequestParser()
        parser.add_argument("name", required=True, help="权限名称")
        parser.add_argument("parentId", required=True, help="父id不能为空")
        parser.add_argument("type")
        parser.add_argument("permissionKey")
        parser.add_argument("menuUrl")
        parser.add_argument("icon")

        args = parser.parse_args()
        name = args.name
        parent_id = args.parentId
        permission_type = args.type
        permission_key = args.permissionKey
        menu_url = args.menuUrl
        icon = args.icon

        permission = AuthPermission(name=name, parent_id=parent_id, type=permission_type,
                                    permission_key=permission_key, menu_url=menu_url, icon=icon)
        db.session.add(permission)
        db.session.commit()
        current_app.logger.info(f"新增{name}权限成功")
        return {"code": 200, "message": True, "message": f"新增{name}权限成功"}

    except (Exception, OperationalError, SQLAlchemyError) as e:
        current_app.logger.error(e.__str__())
        db.session.rollback()
        return FBMsg.create_permission_error_message()


def update_permission(permission_id):
    permission = AuthPermission.query.filter_by(id=permission_id, is_delete=0).first()
    if not permission:
        return FBMsg.error_param()

    parser = reqparse.RequestParser()
    parser.add_argument("name", required=True, help="权限名称")
    parser.add_argument("parentId", required=True, help="父id不能为空")
    parser.add_argument("type")
    parser.add_argument("permissionKey")
    parser.add_argument("menuUrl")
    parser.add_argument("icon")

    args = parser.parse_args()
    name = args.name
    parent_id = args.parentId
    permission_type = args.type
    permission_key = args.permissionKey
    menu_url = args.menuUrl
    icon = args.icon

    permission.name = name if name else permission.role_name
    permission.parent_id = parent_id if parent_id else permission.role_key
    permission.permission_type = permission_type if permission_type else permission.role_key
    permission.permission_key = permission_key if permission_key else permission.role_key
    permission.menu_url = menu_url if menu_url else permission.role_key
    permission.icon = icon if icon else permission.role_key

    db.session.add(permission)
    db.session.commit()
    current_app.logger.info(f"修改permission_id为{permission_id}成功")
    return {"code": 200, "success": True, "message": "修改permission成功"}


def delete_permission(permission_id):
    permission = AuthPermission.query.filter_by(id=permission_id, is_delete=0).first()
    if not permission:
        return FBMsg.error_param()
    permission.is_delete = 1

    db.session.add(permission)
    db.session.commit()
    current_app.logger.info(f"删除permission_id为{permission_id}成功")
    return {"code": 200, "success": True, "message": "删除permission成功"}


def disable_or_enable_permission():
    permission_id = request.json.get("permissionId", 0)
    if not permission_id:
        return FBMsg.error_param()
    permission = AuthPermission.query.filter_by(id=permission_id, is_delete=0).first()
    if not permission:
        return FBMsg.error_param()
    permission.status ^= 1

    db.session.add(permission)
    db.session.commit()
    current_app.logger.info(f"改变permission_id为{permission_id}的状态成功")
    return {"code": 200, "success": True, "message": f"改变permission_id为{permission_id}的状态成功"}


def show_or_hide_permission():
    permission_id = request.json.get("permissionId", 0)
    if not permission_id:
        return FBMsg.error_param()
    permission = AuthPermission.query.filter_by(id=permission_id, is_delete=0).first()
    if not permission:
        return FBMsg.error_param()
    permission.show ^= 1

    db.session.add(permission)
    db.session.commit()
    current_app.logger.info(f"改变permission_id为{permission_id}的展示状态成功")
    return {"code": 200, "success": True, "message": f"改变permission_id为{permission_id}的展示状态成功"}